package ru.ifmo.filemanager.filesystem;

import java.io.IOException;

import ru.ifmo.filemanager.filesystem.interfaces.Directory;
import ru.ifmo.filemanager.filesystem.interfaces.DirectoryLookupReceiver;
import ru.ifmo.filemanager.filesystem.interfaces.FileSystemObject;

public class DirectoryLookupTask implements Runnable {
	
	protected final Directory self;
	protected final String name;
	protected final DirectoryLookupReceiver receiver;
	
	public DirectoryLookupTask(Directory self, String name, DirectoryLookupReceiver receiver) {
		this.self = self;
		this.name = name;
		this.receiver = receiver;
	}

	public void run() {
		run(self, name, receiver);
	}

	public static void run(Directory self, String name,
		DirectoryLookupReceiver receiver) {
		FileSystemObject result;
		try {
			result = self.lookup(name);
		} catch (IOException error) {
			receiver.directoryLookupFailure(self, name, error);
			return;
		}
		receiver.directoryLookupSuccess(self, name, result);
	}
}